#!/bin/bash

echo '安装基础工具>>>>>>>>>>>>>>>>'
sudo yum install -y yum upgrade
sudo yum install -y net-tools
sudo yum install -y git vim gcc glibc-static telnet 
sudo yum install -y bridge-utils net-tools libvirt-devel curl
sudo yum install -y wget

echo '提前修改root密码>>>>>>>>>>>>>>sudo passwd'

echo '###############防火墙设置#############'

systemctl disable firewalld
systemctl stop firewalld
systemctl status firewalld

echo '###############禁用swap#############'
swapoff -a
sed -i 's/.*swap.*/#&/' /etc/fstab

echo '禁用selinux>>>>>>>>>>'
setenforce 0
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config

echo '###############网络设置#############'
cat > /etc/modules-load.d/docker.conf <<EOF
overlay
br_netfilter
EOF
modprobe overlay
modprobe br_netfilter

echo '使桥接流量对iptables可见>>>>>>>'

cat > /etc/sysctl.d/k8s.conf <<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF

sysctl --system


cat  > /etc/security/limits.d/90-nproc.conf <<EOF
*          soft    nproc     20480
*          hard    nproc     20480
*          soft    nofile    102400
*          hard    nofile    102400
root       soft    nproc     unlimited
EOF

#验证是否生效，均返回 1 即正确
# sysctl -n net.bridge.bridge-nf-call-iptables
# sysctl -n net.bridge.bridge-nf-call-ip6tables

echo ' 配置时钟服务器[所有节点执行]>>>>>>>>>>>>>'



echo '###############安装docker#############'

rm -rf /etc/yum.repos.d/docker-ce.repo
wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum makecache fast
echo '###############查看仓库#############'
yum repolist
# yum list docker-ce.x86_64 --showduplicates | sort -r
yum install -y docker-ce-19.03.12-3.el7

echo '###############docker配置#############'

#cp -f /etc/docker/daemon.json /etc/docker/daemon.json`date '+%Y%m%d%H%M%S'`
rm  -rf /etc/docker/daemon.json
mkdir -p /etc/docker
touch /etc/docker/daemon.json

cat > /etc/docker/daemon.json <<EOF
{
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-opts": {"max-size": "100m"},
  "data-root": "/data/docker",
  "registry-mirrors": [
    "https://gqjesmn6.mirror.aliyuncs.com",
    "https://hub-mirror.c.163.com",
    "https://mirror.baidubce.com"
  ]
}
EOF

docker info
systemctl start docker
systemctl enable docker
systemctl restart docker

echo '###############docker-Repository信息#############'
docker images --format "{{.Repository}}:{{.Tag}}"
 
echo '###############docker信息#############'
docker info


echo '###############安装kubeadm、kubelet、kubectl#############'

echo '配置k8s源>>>>>>>>>>>>>>>'
cat > /etc/yum.repos.d/kubernetes.repo <<EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF

rm -rf ./rpm-package-key.gpg
wget https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
rpm --import rpm-package-key.gpg
yum makecache fast

# yum search  kubelet --showduplicates
# yum search  kubeadm --showduplicates
# yum search  kubectl --showduplicates
yum install -y kubelet-1.18.5-0 kubeadm-1.18.5-0  kubectl-1.18.5-0 


systemctl enable kubelet.service
systemctl start kubelet.service
systemctl status kubelet.service


echo '###############配置/etc/sysctl.d/k8s.conf#############'
cat  >  /etc/sysctl.d/k8s.conf <<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
vm.swappiness=0
EOF
sysctl --system

cat  > /etc/sysconfig/kubelet << EOF
KUBELET_EXTRA_ARGS=--fail-swap-on=false
EOF



# cat /proc/sys/net/bridge/bridge-nf-call-ip6tables
# cat /proc/sys/net/bridge/bridge-nf-call-iptables

echo "KUBELET_EXTRA_ARGS=--fail-swap-on=false" > /etc/sysconfig/kubelet
sysctl -p /etc/sysctl.d/k8s.conf
echo 'KUBECONFIG=/etc/kubernetes/admin.conf' >> /etc/profile
source /etc/profile


######################master-手动执行###############################

echo '拉取镜像，开始安装主节点>>>>>>>>>>>>>>>>>>>>>>>'
kubeadm config images pull \
 --kubernetes-version=v1.18.5 \
 --image-repository registry.aliyuncs.com/google_containers


echo '安装主节点>>>>>>>>>>>>>>>>>>>>>>'

export KUBECONFIG=/etc/kubernetes/admin.conf

 kubeadm init  --image-repository registry.aliyuncs.com/google_containers \
 --kubernetes-version=v1.18.5 \
 --pod-network-cidr=10.244.0.0/16 \
 --service-cidr=10.96.0.0/12 \
 --apiserver-advertise-address=192.168.20.110

echo '设置config[master节点执行]>>>>>>>>>>>>>>>'
echo '切换到相应的账户下，进行操作>>>>>>>>>>>>'
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

echo '主节点安装网络fannel插件>>>>>>>>>>>>>>'
kubectl apply -f ./kube-flannel-aliyun.yml

echo '单机运行>>>>>>>>>kubectl taint nodes'
kubectl taint nodes --all node-role.kubernetes.io/master-

systemctl restart kubelet.service

######################master###############################




######################集群node###############################


# kubeadm token create `kubeadm token generate` --print-join-command --ttl=0  

# kubeadm config images pull \
#  --kubernetes-version=v1.18.5 \
#  --image-repository registry.aliyuncs.com/google_containers

# kubeadm join 10.0.2.15:6443 --token thvuiu.fosv0aipfpjkcc54 \
#     --discovery-token-ca-cert-hash sha256:424e9e0e1ba443071995c479ae8dd20f4b33fdc758a02bc2f2bc0d2662d1565e


# systemctl restart kubelet.service
# echo '集群标签修改>>>>>>>>>>>>>>>>>'
# kubectl label nodes k8s-node1  node-role.kubernetes.io/worker=

######################node###############################
echo '安装openebs'
# https://blog.csdn.net/networken/article/details/105790184
# http://t.zoukankan.com/keithtt-p-13197080.html
# wget https://openebs.github.io/charts/openebs-operator.yaml
# sed -i 's#quay.io#docker.io#g' openebs-operator.yaml
# kubectl apply -f openebs-operator.yaml

echo '安装青云K8s管理工具，https://kubesphere.com.cn/docs/quick-start/minimal-kubesphere-on-k8s/'

echo  '30880(admin/P@88w0rd)'


